package com.sdonkey.score.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.sdonkey.score.bean.AdInfoBean;
import com.sdonkey.score.service.IMyAdsService;
import com.sdonkey.score.util.FormValidateUtil;
import com.sdonkey.score.util.MD5;
import com.sdonkey.score.util.StaticVariable;
import com.sdonkey.score.util.StringUtil;

/**
 * 我的广告
 * @author 窦靖禹、赵超
 * @email 773134958@qq.com
 */
@Controller
@RequestMapping(value = "/myAds", method = RequestMethod.POST)
public class MyAdsController {

    @Autowired
    private IMyAdsService myAdsService;



    // --------------------------------应用广告----------------------------

    /**
     * 应用广告首页数据加载
     * @param session
     * @param page
     * @param status    是否完成标记 完成为1  未完成为0
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxGetUserAppList")
    public Map<String, Object> ajaxGetUserAppList(HttpSession session, String page, String status) throws Exception {
        if (FormValidateUtil.isEmpty(status)) {
            status = "0";
        }
        String userId = session.getAttribute("id").toString();
        int pageNum;
        try {
            pageNum = Integer.valueOf(page);
        } catch (Exception e) {
            pageNum = 1;
        }
        Map<String, Object> userAppList = myAdsService.getUserAppList(userId, pageNum, status);
        return userAppList;
    }

    /**
     * 条件查询 应用列表
     *
     * @param condition
     * @param page
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxSelectAppByCondition")
    public Map<String, Object> ajaxSelectAppByCondition(HttpSession session, String condition, String page) throws Exception {
        String userId = session.getAttribute("id").toString();
        int pageNum;
        try {
            pageNum = Integer.valueOf(page);
        } catch (Exception e) {
            pageNum = 1;
        }
        return myAdsService.selectAppByCondition(userId, condition, pageNum);
    }

    /**
     * 绑定在该应用平台所注册账号
     * @param app_id
     * @param account
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxBindAppAccount")
    public int ajaxBindAppAccount(HttpSession session, String app_id, String account) throws Exception {
        String userId = session.getAttribute("id").toString();
        return myAdsService.bindAppAccount(userId, app_id, account);
    }

    /**
     * 查询单个应用（渠道）的广告列表
     * @param app_id 应用id
     * @param ad_type 广告类型 v视频 p图片
     * @param page
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxSelectOneAppAds")
    public Map<String, Object> ajaxSelectOneAppAds(HttpSession session, String app_id, String ad_type, String page) throws Exception {
        int pageNum;
        try {
            pageNum = Integer.valueOf(page);
        } catch (Exception e) {
            pageNum = 1;
        }
        String userId = session.getAttribute("id").toString();
        return myAdsService.selectOneAppAds(userId, app_id, ad_type, pageNum);
    }

    // -----------------------------内容商专属广告----------------------------------

    /**
     * 内容商专属广告数据加载
     *
     * @param session
     * @param page
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxdeveloper_Ads")
    public Map<String, Object> developer_Ads(HttpSession session, String page) throws Exception {
        String userId = session.getAttribute("id").toString();
        if (FormValidateUtil.isEmpty(page)) {
            page = "1";
        }
        int intpage = Integer.parseInt(page);
        return myAdsService.selectDeveloper_Ads(userId, intpage);
    }

    /**
     * 再次播放 跳转到该内容商的所有广告页面
     * @param session
     * @param devId
     * @param page
     * @param flag    标记需要video 还是picture "v"=video "p"=picture
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxplayAgain")
    public Map<String, Object> playAgain(HttpSession session, String devId, String page, String flag) throws Exception {
        String userId = session.getAttribute("id").toString();
        if (FormValidateUtil.isEmpty(page)) {
            page = "1";
        }
        int intpage = Integer.parseInt(page);
        return myAdsService.selectDeveloper_Ads(userId, devId, flag, intpage);
    }

    // -----------------------------我的订阅广告----------------------------------

    /**
     * 查询广告主
     *
     * @param session
     * @param page    当前页数
     * @return 存在map里的当前页数，总页数，用户对象
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxmyads")
    public Map<String, Object> selectMyAds(HttpSession session, Integer page) throws Exception {
        String devId = session.getAttribute("id").toString();
        if (page == null) {
            page = 1;
        }
        Page<AdInfoBean> page1 = myAdsService.selectMyAds(Integer.valueOf(devId), page,
                StaticVariable.advister_page_num);
        Map<String, Object> map = new HashMap<>();
        map.put("nowpage", page1.getPageNum());
        map.put("sumpage", page1.getPages());
        map.put("advistersList", page1.getResult());
        return map;
    }

    /**
     * @param adv_id
     * @param ad_type
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping("/ajaxmyad")
    @ResponseBody
    public Map<String, Object> selectAd(Integer adv_id, String ad_type, Integer page) throws Exception {
        if (FormValidateUtil.isEmpty(ad_type)) {
            ad_type = "video";
        }
        if (page == null) {
            page = 1;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        Page<AdInfoBean> page1 = myAdsService.selectAd(adv_id, ad_type, page, StaticVariable.ad_page_num);
        List<AdInfoBean> list = page1.getResult();
        map.put("nowpage", page1.getPageNum());
        map.put("sumpage", page1.getPages());
        map.put("adsList", list);
        return map;
    }

    // -----------------------------我的收藏广告----------------------------------

    /**
     * 查询已经收藏的所有广告
     *
     * @param session
     * @param ad_type
     * @param page    广告类型
     * @return 已经收藏得所有广告
     */
    @ResponseBody
    @RequestMapping("/ajaxmyCollection")
    public Map<String, Object> selectMyCollectionAds(HttpSession session, String ad_type, Integer page)
            throws Exception {
        String userId = session.getAttribute("id").toString();
        if (page == null) {
            page = 1;
        }
        if (ad_type == null || ad_type == "") {
            ad_type = "video";
        }
        Map<String, Object> map = new HashMap<String, Object>();
        Page<AdInfoBean> page1 = myAdsService.selectMyCollectionAds(userId, ad_type, page, StaticVariable.ad_page_num);
        List<AdInfoBean> list = page1.getResult();
        map.put("nowpage", page1.getPageNum());
        map.put("sumpage", page1.getPages());
        map.put("adslist", list);
        return map;
    }

    // -----------------------------我的内容商----------------------------------

    @RequestMapping("/myDevelopers")
    public String to_myDevelopers() throws Exception {
        return "/myDevelopers";
    }

    /**
     * 我的内容商 展示出我关注的所有内容商
     *
     * @param session
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping("/ajaxmyDevelopers")
    public Map<String, Object> myDevelopers(HttpSession session, String page) throws Exception {
        String userId = session.getAttribute("id").toString();
        if (FormValidateUtil.isEmpty(page)) {
            page = "1";
        }
        int intpage = Integer.parseInt(page);
        return myAdsService.selectMyDevelopers(userId, intpage);
    }


    @RequestMapping("/searchDevelopers")
    public String to_searchDevelopers() throws Exception {
        return "/searchDevelopers";
    }

    /**
     * 查找内容商
     *
     * @param session
     * @param info    查询内容
     * @param page
     * @param first   内容商首字母
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxsearchDevelopers")
    public Map<String, Object> searchDevelopers(HttpSession session, String info, String first, String page)
            throws Exception {
        String userId = session.getAttribute("id").toString();
        if (FormValidateUtil.isEmpty(page)) {
            page = "1";
        }
        int intpage = Integer.parseInt(page);
        return myAdsService.searchDevelopers(userId, info, first, intpage);
    }

    /**
     * 添加内容商（添加关注）
     *
     * @param session
     * @param devId
     * @return
     */
    @ResponseBody
    @RequestMapping("/ajaxAddDeveloper")
    public String addDeveloper(HttpSession session, String devId) throws Exception {
        String userId = session.getAttribute("id").toString();
        int flag = myAdsService.addDeveloper(userId, devId);
        if (flag == 1) {
            return "1";
        } else {
            return "-1";
        }
    }

    @RequestMapping("/developerDetails")
    public String to_developerDetails() throws Exception {
        return "/developerDetails";
    }

    /**
     * 查看内容商（详情） 未关注/已关注
     *
     * @param session
     * @param devId
     * @param page
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxdeveloperDetails")
    public Map<String, Object> developerDetails(HttpSession session, String devId, String page) throws Exception {
        String userId = session.getAttribute("id").toString();
        return myAdsService.selectDevelopers(userId, devId, 0, null);
    }

    /**
     * 删除内容商（取消关注）
     *
     * @param session
     * @param devId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxDeleteDeveloper")
    public String deleteDeveloper(HttpSession session, String devId) throws Exception {
        String userId = session.getAttribute("id").toString();
        System.out.print(userId + "-----------------------------------");
        int flag = myAdsService.deleteDeveloper(userId, devId);
        if (flag == 1) {
            return "1";
        } else {
            return "-1";
        }
    }

    /**
     * 内容商详情 => 更多
     *
     * @param session
     * @param devId
     * @param flag    "v"=video "p"=picture "a"=application
     * @param page
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/ajaxdeveloper_Ads_More")
    public Map<String, Object> developer_Ads_More(HttpSession session, String devId, String flag, String page)
            throws Exception {
        String userId = session.getAttribute("id").toString();
        if (FormValidateUtil.isEmpty(page)) {
            page = "1";
        }
        int intpage = Integer.parseInt(page);
        Map<String, Object> map = myAdsService.selectDevelopers(userId, devId, intpage, flag);
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("developer", map.get("developer"));
        if ("v".equals(flag)) {
            map1.put("developer_Ads_videoList", map.get("developer_Ads_videoList"));
        } else if ("p".equals(flag)) {
            map1.put("developer_Ads_pictureList", map.get("developer_Ads_pictureList"));
        } else if ("a".equals(flag)) {
            map1.put("applicationList", map.get("applicationList"));
        }
        map1.put("nowpage", map.get("nowpage"));
        map1.put("sumpage", map.get("sumpage"));
        return map1;
    }

}
